﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace nExt.Core.Extensions
{
	/// <summary>
	/// Hilfsklasse für Validierung
	/// </summary>
	public static class Validation
	{
		/// <summary>
		/// Validiert ein Object auf die gesetzten DataAnnotations
		/// </summary>
		public static IEnumerable<ValidationResult> Validate(this object obj)
		{
			var context = new ValidationContext(obj, null, null);
			var result = new List<ValidationResult>();
			Validator.TryValidateObject(obj, context, result, true);
			return result;
		}

		/// <summary>
		/// Validiert eine Eigenschaft eines Objekts auf die gesetzten DataAnnotations
		/// </summary>
		public static IEnumerable<ValidationResult> ValidateProperty(this object obj, string propertyName)
		{
			//var context = new ValidationContext(obj, null, null);
			//context.MemberName = propertyName;
			//var result = new List<ValidationResult>();
			//Validator.TryValidateProperty(obj, context, result);
			return Validate(obj).Where(result => result.MemberNames.Contains(propertyName));
		}
	}
}